package com.hos.controller;



import com.hos.framework.Result;
import com.hos.framework.ResultBean;
import com.hos.framework.ResultUtil;
import com.hos.pojo.Doctor;
import com.hos.service.Card001Service;
import com.hos.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Liutianci
 */
@RestController
@RequestMapping("/api/doctor")
public class DoctorController {
    @Autowired
    DoctorService doctorService;
    @Autowired
    Card001Service card001Service;

    @PostMapping("/save")
    public Result<String> modify(@RequestBody Doctor entity) {
        int count = doctorService.checkIsbn(entity);
        if(count>0){
            return ResultUtil.fail("该卡号已存在！");
        }
        if(entity.getId()!=null){
            doctorService.modify(entity);
        }else{
            doctorService.add(entity);
        }

        return ResultUtil.ok();
    }
    @PostMapping("/remove")
    public Result<String> remove(@RequestBody Integer[] ids) {
        doctorService.removes(ids);
        return ResultUtil.ok();
    }

    @GetMapping("/list")
    public ResultBean<List<Doctor>> getPageResult(
            @RequestParam(required = false) Integer docid,
            @RequestParam(required = false) String docname,
            @RequestParam(required = false) String doctype,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {

        Map<String, Object> param = new HashMap<>(16);

        // 计算起始行号
        int offset = (page - 1) * limit;
        int rows = limit;

        param.put("docid",docid);
        param.put("docname",docname);
        param.put("doctype",doctype);
        param.put("offset", offset);
        param.put("rows", rows);

        // 统计记录数
        int totalRows = doctorService.count(param);

        // 获取当前页结果集
        List<Doctor> entities = doctorService.getPageResult(param);

        for (Doctor entity : entities) {
            if (card001Service.selectCardToday(entity.getDocid())!=null){
                entity.setIspost(1);
            }else {
                entity.setIspost(2);
            }
        }
        return new ResultBean<>(0, "查询成功", totalRows, entities);

    }


}
